//
//  HQBaseViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/4.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQBaseViewController.h"

@interface HQBaseViewController ()<UIGestureRecognizerDelegate>

@end

@implementation HQBaseViewController

-(void)sourceLabel:(UILabel *)sourceLabel withTitle:(NSString *)title withData:(NSString *)data withData2:(NSString *)data2{
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@%@",title,data,data2]];
    
    [attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0,[title length])];
    [attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange([title length],[data length])];
    [attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange([title length]+[data length],[data2 length])];
    sourceLabel.attributedText = attributeStr;
}

-(void)sourceLabel:(UILabel *)sourceLabel withTitle:(NSString *)title withData:(NSString *)data{
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@",title,data]];
    [attributeStr addAttribute:NSForegroundColorAttributeName value:[self colorHexValue:0x647180] range:NSMakeRange(0,[title length])];
    [attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0,[title length])];
    [attributeStr addAttribute:NSForegroundColorAttributeName value:[self colorHexValue:0x233040] range:NSMakeRange([title length],[data length])];
    [attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange([title length],[data length])];
    sourceLabel.attributedText = attributeStr;
}


-(void)addSearchBar:(NSString *)placeHolder{
    s = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-44-20, 30)];
    s.placeholder = placeHolder;
    
    UITextField * searchField = [s valueForKey:@"_searchField"];
    [searchField setValue:[UIFont boldSystemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];


    
    s.layer.borderWidth = 2.f;
    s.layer.cornerRadius = 15;
    s.delegate = self;
    s.layer.borderColor = [self colorHexValue:0xF9FCFC].CGColor;
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    [view addSubview:s];
    self.navigationItem.titleView = view;
    self.navigationItem.titleView.frame = CGRectMake(20, 0, SCREEN_WIDTH, 30);
}

-(void)addSearchBar2:(NSString *)placeHolder{
    s = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-20, 30)];
    s.placeholder = placeHolder;
    s.layer.borderWidth = 2.f;
    s.layer.cornerRadius = 15;
    s.delegate = self;
    s.layer.borderColor = [self colorHexValue:0xF9FCFC].CGColor;
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    [view addSubview:s];
    self.navigationItem.titleView = view;
    self.navigationItem.titleView.frame = CGRectMake(20, 0, SCREEN_WIDTH, 30);
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [s resignFirstResponder];
}


-(void)addTopSearch{
    UIButton *topView = [UIButton buttonWithType:UIButtonTypeCustom];
    topView.frame = CGRectMake(10, 0, [UIScreen mainScreen].bounds.size.width - 20, 35);
    topView.layer.cornerRadius = 17.5f;
    topView.backgroundColor = [self colorHexValue:0xF9FCFC];
    topView.layer.borderColor = [self colorHexValue:0xEAEFFC].CGColor;
    topView.layer.borderWidth = 1.f;
    [topView setImage:[UIImage imageNamed:@"home_search_icon"] forState:UIControlStateNormal];
    [topView setTitle:@"搜索日期和客户电话" forState:UIControlStateNormal];
    [topView setTitleColor:[self colorHexValue:0x98A4B3] forState:UIControlStateNormal];
    topView.titleLabel.font = [UIFont systemFontOfSize:13];
    self.navigationItem.titleView = topView;
}

-(void)addTopSearch:(NSString *)string{
    UIButton *topView = [UIButton buttonWithType:UIButtonTypeCustom];
    topView.backgroundColor = [UIColor redColor];
    topView.frame = CGRectMake(-10, 0, [UIScreen mainScreen].bounds.size.width, 35);
    topView.layer.cornerRadius = 17.5f;
    topView.backgroundColor = [UIColor redColor];
//    topView.backgroundColor = [self colorHexValue:0xF9FCFC];
//    topView.layer.borderColor = [self colorHexValue:0xEAEFFC].CGColor;
    topView.layer.borderWidth = 1.f;
    [topView setImage:[UIImage imageNamed:@"home_search_icon"] forState:UIControlStateNormal];
    [topView setTitle:string forState:UIControlStateNormal];
    [topView setTitleColor:[self colorHexValue:0x98A4B3] forState:UIControlStateNormal];
    topView.titleLabel.font = [UIFont systemFontOfSize:13];
    self.navigationItem.titleView = topView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     *  根据判断是否是根控制器，添加返回按钮
     */
    UIViewController *root = self.navigationController.viewControllers[0];
    
    // 如果不是栈底控制器（根控制器），才需要设置返回按钮
    if (self != root) {
        [self addGesture];
        [self addBackItem];
    }
    
    // 导航条基本设置
    [self navgationBarBaseSetting];
}

-(void)addGesture{
    id target = self.navigationController.interactivePopGestureRecognizer.delegate;
    // handleNavigationTransition:为系统私有API,即系统自带侧滑手势的回调方法，我们在自己的手势上直接用它的回调方法
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    panGesture.delegate = self; // 设置手势代理，拦截手势触发
    [self.view addGestureRecognizer:panGesture];
    
    // 一定要禁止系统自带的滑动手势
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

// 什么时候调用，每次触发手势之前都会询问下代理方法，是否触发
// 作用：拦截手势触发
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    // 当当前控制器是根控制器时，不可以侧滑返回，所以不能使其触发手势
    if(self.navigationController.childViewControllers.count == 1){
        return NO;
    }
    return YES;
}

#pragma mark 返回手势
- (void)handleNavigationTransition:(UIGestureRecognizer *)goBack{
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 添加键盘事件监听
 */
-(void)addKeyBoardObserver{
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
    
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{
    
}

/**
 *  导航条显示基本设置
 */
-(void)navgationBarBaseSetting
{
    // 1、导航条标题设置
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[self navgationTitleColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:55/3.0f]}];
    
    // 2、使用导航条高度
    self.navigationController.navigationBar.translucent = NO;
    
    // 2、使用导航条高度
    self.tabBarController.tabBar.translucent = NO;
    
    // 3、设置导航条颜色
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    
    // 4、状态栏采用黑色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

//
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

/**
 *  添加自定义返回按钮
 */
-(void)addBackItem{
    UIButton *releaseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    releaseButton.backgroundColor = [UIColor clearColor];
    releaseButton.frame = CGRectMake(0, 0, 25, 44);
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,(44-20)/2, 12.5, 20)];
    imageView.image = [UIImage imageNamed:NAV_BAKC_IMG];
    [releaseButton addSubview:imageView];
    [releaseButton addTarget:self action:@selector(backItem) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:releaseButton];
}

/**
 *  自定义返回按钮触发的方法
 */
-(void)backItem
{
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 *  显示弹出提示框
 */
-(void)tostView:(NSString *)text{
    [self.view makeToast:text duration:3.0f position:@"center"];
}

/**
 *  显示加载提示
 */
-(void)showHudText:(NSString *)text{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = text;
}

/**
 *  显示通用加载提示
 */
-(void)showHudPbulic{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"加载中...";
}

/**
 *  隐藏Loding
 */
-(void)hiddnHud{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

-(void)hiddenErrorHud{
    [self tostView:@"请检查您的网络连接"];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

/**
 *  设置空数据
 */
-(void)isBlankData:(UITableView *)tableView withData:(NSArray *)dataSource
{
    if (dataSource.count > 0) {
        tableView.backgroundView = nil;
    }else{
        NSString *name = @"no_data";
        UIImage *imageview = [UIImage imageNamed:name];
        UIView *blank = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        blank.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
        UIImageView *blankimg = [[UIImageView alloc]initWithFrame:CGRectMake((tableView.bounds.size.width - imageview.size.width)/2, (tableView.bounds.size.height - imageview.size.height)/2*1-40, imageview.size.width,imageview.size.height)];
        blankimg.image = [UIImage imageNamed:name];
        [blank addSubview:blankimg];
        tableView.backgroundView = blank;
    }
}

/**
 *  给列表添加数据为空提示
 */
-(void)addNoneDataView:(UITableView *)tableView withText:(NSString *)text{
    UIView *backgroundView = [[UIView alloc]initWithFrame:tableView.bounds];
    UILabel *textLabel = [[UILabel alloc]initWithFrame:backgroundView.bounds];
    textLabel.text = text;
    textLabel.textColor = [UIColor whiteColor];
    textLabel.font = [UIFont systemFontOfSize:16];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.numberOfLines = 0;
    [backgroundView addSubview:textLabel];
    tableView.backgroundView = backgroundView;
}

/**
 *  给列表添加数据为空提示
 */
-(void)addNoneDataCollView:(UICollectionView *)collectionView withText:(NSString *)text{
    UIView *backgroundView = [[UIView alloc]initWithFrame:collectionView.bounds];
    UILabel *textLabel = [[UILabel alloc]initWithFrame:backgroundView.bounds];
    textLabel.text = text;
    textLabel.textColor = [UIColor whiteColor];
    textLabel.font = [UIFont systemFontOfSize:16];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.numberOfLines = 0;
    [backgroundView addSubview:textLabel];
    collectionView.backgroundView = backgroundView;
}

-(void)setPlaceHolderColor:(UITextField *)textField{
    textField.tintColor = [UIColor whiteColor];
    UIColor * color = [UIColor colorWithRed:255/255.0f green:254/255.0f blue:254/255.0f alpha:0.8];
    [textField setValue:color forKeyPath:@"_placeholderLabel.textColor"];
}

/**
 判断用户输入是否为空
 
 @param inputStr 输入字符串
 @return 是否正确
 */
-(BOOL)isInputEmpty:(NSString *)inputStr{
    if([inputStr isEqualToString:@""]){
        return YES;
    }else {
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimedString = [inputStr stringByTrimmingCharactersInSet:set];
        if ([trimedString length] == 0) {
            return YES;
        } else {
            return NO;
        }
    }
}

@end
